package org.example;

import java.util.Date;

public enum MonthToDayEnums {
    JANUARY(1, 31),
    March(3, 31),
    April(4, 30),
    May(5, 31),
    June(6, 30),
    July(7, 31),
    August(8, 31),
    September(9, 30),
    October(10, 31),
    November(11, 30),
    DECEMBER(12, 31);
    private int month;
    private int day;

    MonthToDayEnums(int month, int day) {
        this.month = month;
        this.day = day;
    }

    /**
     * 判断闰年还是平年
     *
     * @param year
     * @return
     */
    public static boolean judgeYear(int year) {
        // 闰年 366
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return true;
        }
        // 平年 365
        return false;
    }

    /**
     * 根据年份判断某月有多少天
     *
     * @param year
     * @param month
     * @return
     */
    public static int monthToDay(int year, int month) {
        int result = 0;
        // 2 月份特殊判断,其余直接返回枚举类
        if (month == 2) {
            // 闰年
            result = (judgeYear(year)) ? 29 : 28;
        } else {
            for (MonthToDayEnums item : values()) {
                if (item.month == month) {
                    result = item.day;
                    break;
                }
            }
        }
        return result;
    }

    public static int calDay(Date date) {
        int y = date.getYear();
        int m = date.getMonth();
        int d = date.getDay();
        int sum=0;
        for (int i=1;i<=m-1;++i){
             sum+=monthToDay(y,m);
        }
        return sum+d;
    }



}
